<template>
  <div class="top-category" v-if="localItem">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="fadeBread" mode="out-in">
          <!-- key值的作用:     -->
          <XtxBreadItem :key="localItem ? localItem.id : '111'">
            {{ localItem ? localItem.name : '' }}
          </XtxBreadItem>
        </transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height:500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in localItem.children" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in list" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.desc }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, ref, defineAsyncComponent, watch } from 'vue'
import { useStore } from 'vuex'
import { onBeforeRouteLeave, useRoute } from 'vue-router'
import { category } from '@/api/category'
export default {
  name: 'TopCategory',
  components: {
    GoodsItem: defineAsyncComponent(() => import('./components/goods-item.vue'))
  },
  setup () {
    // 轮播图
    const sliders = ref([])
    const store = useStore()
    const route = useRoute()
    const localItem = computed(() => {
      // find:找到了就返回当前项item,找不到就返回undef
      return store.state.category.list.find(item => {
        return item.id === route.params.id
      })
    })
    // 轮播图数据处理
    store.dispatch('home/getBannerList').then(res => {
      sliders.value = res
    })
    // 一级分类数据
    const list = ref([])
    // 返回值 (停止侦听的方法)=wacth(侦听值,()=>{},{deep,immediate,flush})
    const stop = watch(
      () => route.params.id,
      newVal => {
        category(newVal).then(res => {
          list.value = res.result.children
        })
      },
      {
        immediate: true
      }
    )
    onBeforeRouteLeave(() => {
      stop()
    })
    return { sliders, localItem, list }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
// .fadeBread-enter-active,
// .fadeBread-leave-active {
//   transition: all 0.5s;
// }
// .fadeBread-enter-from {
//   opacity: 0;
//   transform: translateX(100px);
// }
// .fadeBread-leave-to {
//   opacity: 0;
//   transform: translateX(100px);
// }
 .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
</style>
